package com.hengpeng.itfin.persist.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hengpeng.common.persist.AbstractDao;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.itfin.persist.RedPacketDao;
import com.hengpeng.itfinbase.en.RewardRedPacketStatus;
import com.hengpeng.itfinbase.persist.RewardRedpacket;

@Repository
public class RedPacketDaoImpl extends AbstractDao<RewardRedpacket> implements RedPacketDao {

    @Override
    public List<RewardRedpacket> findRedPacketByStatus(RewardRedPacketStatus status, int fetchSize) {
        DynamicQuery query = new DynamicQuery(RewardRedpacket.class);
        query.eq("status", status);
        if (fetchSize != -1) {
            query.setPageIndex(1);
            query.setPageIndex(fetchSize);
        }
        query.setQueryType(QueryType.JDBC);
        return super.findAllByDynamicQuery(query);
    }

    @Override
    public int updateStatus(long id, RewardRedPacketStatus oldStatus, RewardRedPacketStatus newStatus, Date updateTime) {
        return super.exeNamedQuery("redpacket.updateStatus", new Object[] { newStatus, updateTime, id, oldStatus });
    }

    @Override
    public int shareUpdateRedPacket(Long id, Long shareMoney, RewardRedPacketStatus status, Date updateTime) {
        return super.exeNamedQuery("redpacket.share", new Object[] { shareMoney, updateTime, shareMoney, id, status });
    }

    @Override
    public int shareBackUpdateRedPacket(Long id, Long shareMoney, RewardRedPacketStatus status, Date updateTime) {
        return super.exeNamedQuery("redpacket.shareback", new Object[] { shareMoney, updateTime, id, status });
    }

    @Override
    public int sharedRedpacketUpdate(Long id, Date updateTime, RewardRedPacketStatus newStatus,
            RewardRedPacketStatus oldStatus) {
        return super.exeNamedQuery("redpacket.sharedUpdate", new Object[] { newStatus, updateTime, id, oldStatus });
    }

    @Override
    public List<RewardRedpacket> findRedPacketByStatusAndUser(RewardRedPacketStatus status, String userName) {
        DynamicQuery query = new DynamicQuery(RewardRedpacket.class);
        query.eq("status", status);
        query.eq("userName", userName);
        query.setQueryType(QueryType.JDBC);
        return super.findAllByDynamicQuery(query);
    }

}